package ebcs.scan.controller.proteamCtr;

import com.google.gson.Gson;

import ebcs.database.bean.BsGroupInfor;
import ebcs.database.bean.BsProTeamInfor;
import ebcs.database.bean.BsProTeamInfor;
import ebcs.database.bean.BsUserDetailInfo;
import ebcs.database.bean.BsUserInfor;
import ebcs.database.bean.RpUserTaskNeedBean;
import ebcs.database.bean.SysFunction;
import ebcs.database.service.BsGroupInforServiceImp;
import ebcs.database.service.BsProTeamServiceImp;
import ebcs.database.service.BsUserInforServiceImp;
import ebcs.database.service.SystemServiceImp;
import ebcs.database.service.impl.BsProTeamServiceImpl;
import ebcs.database.service.impl.BsUserNeedServiceImpl;
import ebcs.scan.common.consts.KLinkUsSysConsts;
import ebcs.scan.common.consts.UserStatus;
import ebcs.scan.common.utils.HttpClientUtil;
import ebcs.scan.common.utils.Properties;
import ebcs.scan.common.utils.StringUtil;
import ebcs.scan.service.wechat.WeChatTemplPushService;
import ebcs.scan.service.wechat.WechatCtrlService;
import ebcs.scan.service.wechat.WechatSignTools;
import jplat.core.session.JSession;
import jplat.core.trans.JAppBaseService;
import jplat.core.trans.JAppContext;
import jplat.core.trans.JAppContextFactory;
import jplat.error.exception.JSystemException;
import jplat.error.exception.JTransException;
import jplat.tools.config.JAppConfig;
import jplat.tools.string.DateUtil;
import jplat.tools.string.JRandomUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/***
 * 项目组基本信息
 * @author chenzhuo
 * @comment
 */
@Controller
public class ProTeamManagerCtrl extends JAppBaseService {
    private Logger logger = Logger.getLogger(ProTeamManagerCtrl.class);


    protected JAppContextFactory serviceFactory = new JAppContextFactory();


    //数据库库服务.

    @Autowired
    public BsUserInforServiceImp userInfoService;
    @Autowired
    public BsProTeamServiceImp teamService;

    @Autowired
    public BsGroupInforServiceImp groupService;

    @Autowired
    public SystemServiceImp systemService;
    @Autowired
    public WechatCtrlService wechatService;

    public static String CHARSET_ENCODING = "UTF-8";



    //项目组后台管理
    //1、项目组信息查询
    @RequestMapping("/page/sys/searchProTeamInfor.do")
    public void searchproTeamInfor(HttpServletRequest request, HttpServletResponse response) throws JTransException, JSystemException {
        JAppContext context = serviceFactory.buildAppContextNoUser(request, response);//自动客户信息
        BsProTeamInfor proTeam = context.obtainReqModel(BsProTeamInfor.class);
        List<BsProTeamInfor> proTeamDetailInfoList = teamService.selectProTeamDetail(proTeam);
        context.buildAppResponse(proTeamDetailInfoList);
    }
    //2 查询出团队信息返回新增项目组页面
    @RequestMapping("/page/sys/addProTeamBefore.do")
    public String addProTeam(HttpServletRequest request, HttpServletResponse response) throws JTransException, JSystemException {
    	List<BsGroupInfor> groupList = groupService.searchGroupList();
    	request.setAttribute("groupList",groupList );
    	return "main/report/reportAddProTeam";
    }
    //3 新增项目组
    @RequestMapping("/page/sys/addSingleProTeam.do")
    public void  addSingleProTeam(HttpServletRequest request, HttpServletResponse response) throws JTransException, JSystemException {
    	JAppContext context = serviceFactory.buildAppContextNoUser(request, response);//自动客户信息
        BsProTeamInfor proTeam = context.obtainReqModel(BsProTeamInfor.class);
        if (StringUtils.isNotBlank(proTeam.getProTeamName())) {
            List<BsProTeamInfor> proTeamInforList = this.teamService.selectProTeamInforsByProTeamName(proTeam.getProTeamName());
            if (!CollectionUtils.isEmpty(proTeamInforList)) {
                for (BsProTeamInfor bsProTeamInfor : proTeamInforList) {
                    throw new JTransException("1001", "项目组名称重复了！");
                }
            }
        }
        List<BsUserInfor> userinfoList= userInfoService.selectUserInforsByPhoneNo(proTeam.getPhoneNo());
        if(userinfoList!=null&&1==userinfoList.size()){
        	proTeam.setUserInfoRouId(userinfoList.get(0).getRouid());
        }else{
        	 throw new JTransException("1001", "号码用户不存在！");
        }
        proTeam.setCreateDate(DateUtil.getSystemTimeBySomeFormat("yyyyMMdd"));
        proTeam.setCreateTime(DateUtil.getTime());
        proTeam.setState("01");
        BsProTeamInfor proTeamTemp=teamService.selectProTeamMaxNoByGroupNo(proTeam.getProGroupNo());
        if(proTeamTemp!=null&&StringUtils.isNotBlank(proTeamTemp.getProTeamNo())&&StringUtils.isNotBlank(proTeamTemp.getRouid())){
        	proTeam.setProTeamNo(proTeamTemp.getProTeamNo());
            proTeam.setRouid(proTeamTemp.getRouid());
        }else{
        	throw new JTransException("1001", "生成项目组信息失败了。");
        }
        int i = this.teamService.insertProTeamInfor(proTeam);
        if (i == 0) {
            throw new JTransException("1001", "新增项目组信息失败了。");
        }
        Map rspModel = new HashMap();
        context.buildAppResponse(rspModel);
    }
    //4 根据proTeamNo查询团队
    @RequestMapping("/page/sys/{proTeamNo}/selectSingleProTeam.do")
    public String selectSingleProTeam(HttpServletRequest request, HttpServletResponse response, @PathVariable String proTeamNo) throws JTransException, JSystemException {
        BsProTeamInfor proTeam = new BsProTeamInfor();
        proTeam.setProTeamNo(proTeamNo);
        List<BsProTeamInfor> detailInfoList = teamService.selectProTeamDetail(proTeam);
        if (!CollectionUtils.isEmpty(detailInfoList)) {
            request.setAttribute("proTeam", detailInfoList.get(0));
        }
        List<BsGroupInfor> groupList = groupService.searchGroupList();
        request.setAttribute("groupList",groupList );
        return "main/report/updateProTeam";
    }
    //5、删除团队信息
    @RequestMapping("/page/sys/deleteSingleProTeam.do")
    public void deleteSingleGroup(HttpServletRequest request, HttpServletResponse response) throws JTransException, JSystemException {
        JAppContext context = serviceFactory.buildAppContextNoUser(request, response);//自动客户信息
        BsProTeamInfor proTeam = context.obtainReqModel(BsProTeamInfor.class);
        int i=teamService.deleteProTeamByProTeamNo(proTeam.getProTeamNo());
        if(i==0){
        	throw new JTransException("1001", "删除项目组失败,请检查是否存在组员状态都是无效！");
        }
        Map rspModel = new HashMap();
        context.buildAppResponse(rspModel);
    }
    @RequestMapping("/page/sys/updateProTeam.do")
    public void updateProTeam(HttpServletRequest request, HttpServletResponse response) throws JTransException, JSystemException {
        JAppContext context = serviceFactory.buildAppContextNoUser(request, response);//自动客户信息
        BsProTeamInfor proTeam = context.obtainReqModel(BsProTeamInfor.class);
        if (StringUtils.isNotBlank(proTeam.getProTeamName())) {
            List<BsProTeamInfor> proTeamInforList = this.teamService.selectProTeamInforsByProTeamName(proTeam.getProTeamName());
            if (!CollectionUtils.isEmpty(proTeamInforList)) {
                for (BsProTeamInfor bsProTeamInfor : proTeamInforList) {
                    if (bsProTeamInfor.getProTeamNo().equals(proTeam.getProTeamNo())) {
                        continue;
                    }
                    throw new JTransException("1001", "项目组名称重复了！");
                }
            }
        }
        List<BsUserInfor> userinfoList= userInfoService.selectUserInforsByPhoneNo(proTeam.getPhoneNo());
        if(userinfoList!=null&&1==userinfoList.size()){
        	proTeam.setUserInfoRouId((userinfoList.get(0).getRouid()));
        }else{
        	 throw new JTransException("1001", "号码用户不存在！");
        }
        int i = this.teamService.updateProTeamByTeamNo(proTeam);
        if (i == 0) {
            throw new JTransException("1001", "更新项目组信息失败了。");
        }
        Map rspModel = new HashMap();
        context.buildAppResponse(rspModel);
    }
    
    //级联更新 
    @RequestMapping("/page/sys/{groupNo}/selectProTeamByGroupNo.do")
    public void selectProTeamByGroupNo(HttpServletRequest request, HttpServletResponse response, @PathVariable String groupNo) throws JTransException, JSystemException {
    	JAppContext context = serviceFactory.buildAppContextNoUser(request, response);//自动客户信息
    	//页面团队下拉选择全选
    	if(groupNo=="all"||groupNo.equals("all")){  
    		groupNo=null;
    	}
        List<BsProTeamInfor> detailInfoList = teamService.selectProTeamByGroupNo(groupNo);
        Map rspModel = new HashMap();
        rspModel.put("teanList", detailInfoList);
        context.buildAppResponse(rspModel);
    }
}
